function mouseMove(hObject,eventdata)
%controls what to do when mouse moves while a button is pressed down

% This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2011. Jose Maria Garcia-Valdecasas Bernal

% 21-Oct-09 ver. 1.0 (splitted from CISNE.m).
drawnow;
handles=guidata(hObject);
grid=handles.TreeWidget.currentNode();
mouseclick=get(handles.figure1,'SelectionType');
if (handles.mouseButtonDown)&&(strcmp(mouseclick,'normal'))
    posax=handles.DisplayWidget.getWidgetPosition();
    p=get(gcf,'CurrentPoint');
    if (p(1,1)-posax(1)<posax(3))&&(p(1,1)>posax(1))
        if (p(1,2)-posax(1,2)<posax(4))&&(p(1,2)>posax(2))
            
            if strcmp(handles.gridAction,'movePoint')
                p=handles.DisplayWidget.currentPoint();
                handles.TreeWidget.setValue('X',p(1,1),grid);
                handles.TreeWidget.setValue('Y',p(1,2),grid);                
                handles.DisplayWidget.drawCPoint(grid,p(1,1),p(1,2));
            elseif strcmp(handles.gridAction,'addCPoint')
                %do nothing, but avoid error when user moves the pointer
                %while adding a new control point.
            else
                p=handles.DisplayWidget.currentPoint();
                if (strcmp(handles.gridAction,'zoomArea'))
                    x0=handles.xp0;
                    y0=handles.yp0;
                    x1=x0;
                    y1=p(1,2);
                    x2=p(1,1);
                    y2=y0;
                    lx=p(1,1)-x1;
                    ly=p(1,2)-y2;
                    x=[x0 x1 p(1,1) x2 x0];
                    y=[y0 y1 p(1,2) y2 y0];
                else
                    if (strcmp(handles.gridAction,'move'))
                        lx=p(1,1)-handles.xp0;
                        ly=p(1,2)-handles.yp0;
                        corners=handles.DisplayWidget.getPointCoord();
                        x0=corners(1,1)+lx;
                        x1=corners(1,2)+lx;
                        x2=corners(1,3)+lx;
                        x3=corners(1,4)+lx;
                        y0=corners(2,1)+ly;
                        y1=corners(2,2)+ly;
                        y2=corners(2,3)+ly;
                        y3=corners(2,4)+ly;
                        handles.TreeWidget.setValue('X0',x0,grid);
                        handles.TreeWidget.setValue('Y0',y0,grid);
                        
                        
                        x=[x0 x1 x2 x3 x0];
                        y=[y0 y1 y2 y3 y0];
                        
                    else
                        if (strcmp(handles.gridAction,'resize'))
                            x0=handles.fixcorner(1);
                            y0=handles.fixcorner(2);
                            
                        else
                            x0=handles.xp0;
                            y0=handles.yp0;
                        end
                        th=handles.TreeWidget.getValue('Angle',grid);
                        
                        modx=abs(p(1,1)-x0)/(p(1,1)-x0);
                        mody=abs(p(1,2)-y0)/(p(1,2)-y0);
                        if(tand(th)~=0)
                            d=sqrt((p(1,1)-x0)^2+(p(1,2)-y0)^2);
                            beta=acosd(abs(p(1,1)-x0)/d);
                            gamma=modx*mody*beta-th;
                            lx=modx*d*cosd(gamma);
                            ly=modx*d*sind(gamma);
                            x1=x0-ly*sind(th);
                            y1=y0+ly*cosd(th);
                            x2=x0+lx*cosd(th);
                            y2=y0+lx*sind(th);
                        else
                            x1=x0;
                            y1=p(1,2);
                            x2=p(1,1);
                            y2=y0;
                            lx=p(1,1)-x1;
                            ly=p(1,2)-y2;
                        end
                        x=[x0 x1 p(1,1) x2 x0];
                        y=[y0 y1 p(1,2) y2 y0];
                        handles.TreeWidget.setValue('X Length',...
                            round(abs(lx)),grid);
                        handles.TreeWidget.setValue('Y Length',...
                            round(abs(ly)),grid);
                    end
                end
                
                handles.DisplayWidget.setTempRect(x,y);
                
            end
            guidata(hObject,handles);
        end
    end
end